//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package common;

public class StringUtils {
  public StringUtils() {
  }

  private static String hexToStr(byte[] dumped, int offset, int len, int blockSize) {
    String result = new String();

    int i;
    for(i = 0; i < blockSize; ++i) {
      if(i < len) {
        result = result + String.format("%02x ", new Object[]{Integer.valueOf(dumped[offset + i] & 255)});
      } else {
        result = result + "   ";
      }
    }

    result = result + "        ";

    for(i = 0; i < len; ++i) {
      int val = dumped[offset + i] & 255;
      if(val < 32 || val > 127) {
        val = 46;
      }

      result = result + String.format("%c", new Object[]{Integer.valueOf(val)});
    }

    return result;
  }

  private static String hexToStr(byte[] dumped, int offset, int len) {
    String result = new String();

    for(int i = 0; i < len; ++i) {
      result = result + String.format("%02x ", new Object[]{Integer.valueOf(dumped[offset + i] & 255)});
    }

    return result;
  }

  public static String hexToString(byte[] dumped, int offset, int len) {
    String result = new String();

    for(int i = 0; i < len; ++i) {
      result = result + String.format("%02x", new Object[]{Integer.valueOf(dumped[offset + i] & 255)});
    }

    return result;
  }

  public static String hexToString(byte[] dumped) {
    return hexToString(dumped, 0, dumped.length);
  }

  public static String macAddrStyle(byte[] data) {
    String mac = new String();

    for(int i = 0; i < data.length; ++i) {
      if(i > 0) {
        mac = mac + ":";
      }

      mac = mac + String.format("%02x", new Object[]{Byte.valueOf(data[i])});
    }

    return mac;
  }

  public static String bytesToString(byte[] bytes) {
    int blocks = bytes.length / 16;
    int last = bytes.length % 16;
    String logstr = new String();

    for(int i = 0; i < blocks; ++i) {
      logstr = logstr + hexToStr(bytes, i * 16, 16) + "\n";
    }

    if(last != 0) {
      logstr = logstr + hexToStr(bytes, blocks * 16, last) + "\n";
    }

    return logstr;
  }

  public static String dumpBytes(byte[] dumped, int offset, int len) {
    int blocks = len / 32;
    int last = len % 32;
    String logstr = new String();

    for(int addr = 0; addr < blocks; ++addr) {
      String addr1 = String.format("  %08x", new Object[]{Integer.valueOf(addr * 32)});
      logstr = logstr + addr1 + ": " + hexToStr(dumped, offset + addr * 32, 32, 32) + "\n";
    }

    if(last != 0) {
      String var8 = String.format("  %08x", new Object[]{Integer.valueOf(blocks * 32)});
      logstr = logstr + var8 + ": " + hexToStr(dumped, offset + blocks * 32, last, 32) + "\n";
    }

    return logstr;
  }

  public static String dumpBytes(byte[] dumped) {
    return dumpBytes(dumped, 0, dumped.length);
  }

  public static byte[] stringToByteArray(String data) {
    String ld = data.replaceAll("(\\s)|(\\t)|(\\n)", "");
    int len = ld.length();
    if(len % 2 != 0) {
      --len;
    }

    byte[] byteData = new byte[len / 2];
    int i = 0;

    for(int rci = 0; i < len; ++rci) {
      byteData[rci] = (byte)Integer.parseInt(ld.substring(i, i + 2), 16);
      i += 2;
    }

    return byteData;
  }
}
